//
//  RunLable.m
//  ZSHUDManager
//
//  Created by 字节科技 on 2018/12/22.
//  Copyright © 2018 Byte. All rights reserved.
//  跑马灯效果

#import "RunLabel.h"
#import "YYWeakProxy.h"
#import "UILabel+Indent.h"
#define RUN_SPACE 20.f

@interface RunLabel ()
@property (nonatomic, assign) CGFloat offsetX; //x偏移量
@property (nonatomic, strong) UILabel *moveLabel;
@property(nonatomic,strong)UILabel *moveLabel2;
@property(nonatomic,strong)CADisplayLink *link;
@end

@implementation RunLabel
- (instancetype)initWithFrame:(CGRect)frame {
    if (self = [super initWithFrame:frame]){
        _speed = 0.7;//默认值
        [self initView];
        [self initTimer];
    }
    return self;
}

- (instancetype)initWithCoder:(NSCoder *)aDecoder{
    self = [super initWithCoder:aDecoder];
    if (self) {
        _speed = 0.7;//默认值
        [self initView];
        [self initTimer];
    }
    return self;
    
}
-(void)initView{
    
    [self changeText:@"未连接" State:RunLabelStateUnKnow];
    [self addSubview:self.moveLabel];
    [self addSubview:self.moveLabel2];
}
//初始化timer
- (void)initTimer{
    self.link = [CADisplayLink displayLinkWithTarget:[YYWeakProxy proxyWithTarget:self] selector:@selector(displayLabelAction)];
    [self.link addToRunLoop:[NSRunLoop currentRunLoop] forMode:NSRunLoopCommonModes];
}

- (void)setTextColor:(UIColor *)textColor{
    self.moveLabel.textColor = textColor;
    _moveLabel2.textColor = textColor;
}

- (void)setFont:(UIFont *)font{
    self.moveLabel.font = font;
    _moveLabel2.font = font;
}

//设置速度
- (void)setSpeed:(CGFloat)speed{
    if (speed < 0) {
        speed = 0;
    }else if (speed > 1){
        speed = 1;
    }
    _speed = speed;
}

//赋值text
- (void)setText:(NSString *)text{
//    if (_moveLabel2) {
//        [_moveLabel2 removeFromSuperview];
//    }
//    if (_moveLabel) {
//        [_moveLabel removeFromSuperview];
//    }
    self.moveLabel.text = text;

    CGFloat textLength = [text sizeWithAttributes:@{NSFontAttributeName:_moveLabel.font}].width;
    self.moveLabel.width = textLength;
    
    _moveLabel.x = RUN_SPACE;
    _offsetX = 0;
    if (textLength>=self.width+RUN_SPACE) {
        self.moveLabel2.text = text;
        self.moveLabel2.width = textLength;
        _moveLabel2.x = CGRectGetMaxX(self.moveLabel.frame)+RUN_SPACE;
        if (![self.subviews containsObject:self.moveLabel2]) {
            [self addSubview:_moveLabel2];
        }
    }else{
        [_moveLabel2 removeFromSuperview];
    }
    
    self.clipsToBounds = YES;//设置了这个属性后才能局部显示
}
-(void)changeText:(NSString *)text State:(RunLabelState)state{
    self.text = text;
    switch (state) {
        case RunLabelStateNormal:
            [self changBackgroundWithColor:RGBColor(0, 255, 0, 0.7)];
            break;
        case RunLabelStateError:
            [self changBackgroundWithColor:RGBColor(255, 0, 0, 0.7)];
            break;
        case RunLabelStateWarning:
            [self changBackgroundWithColor:RGBColor(238, 238, 0, 0.7)];
            break;
        case RunLabelStateUnKnow:
            [self changBackgroundWithColor:RGBColor(130,130, 130, 0.7)];
            break;
            
        default:
            break;
    }
}
-(void)changBackgroundWithColor:(UIColor *)color{
    __block CAGradientLayer *gradientLayer;
    for (CALayer *subLayer in self.layer.sublayers) {
        if ([subLayer isKindOfClass:[CAGradientLayer class]]) {
            gradientLayer = (CAGradientLayer *)subLayer;
        }
    }
    
    
    if (!gradientLayer) {
        gradientLayer = [CAGradientLayer layer];
        gradientLayer.locations = @[@0.2,@0.7];
        gradientLayer.startPoint = CGPointMake(0, 0);
        gradientLayer.endPoint = CGPointMake(1, 0);
        gradientLayer.frame = self.frame;
        [self.layer addSublayer:gradientLayer];
    }
     gradientLayer.colors = @[(__bridge id)color.CGColor, (__bridge id)RGBColor(130, 130, 130, 0.7).CGColor];

   
}
- (void)displayLabelAction{
    CGFloat labelWidth1 =self.moveLabel.width;
    CGFloat labelWidth2 =_moveLabel2.width;
    if (labelWidth1 < self.width+RUN_SPACE) {
        return;//如果字能显示全则不移动
    }
    _offsetX -= self.speed;
    if (_offsetX <= -(labelWidth1+RUN_SPACE)) {
        _moveLabel2.x -= self.speed;
        NSLog(@"第二条展示中");
        //第一条展示完，将label1放到label2的后面
        self.moveLabel.x = CGRectGetMaxX(_moveLabel2.frame)+RUN_SPACE;
    }else{
        NSLog(@"第一条展示中");
        self.moveLabel.x  -= self.speed;
        _moveLabel2.x -= self.speed;
    }
    if (_offsetX<=-(labelWidth1+labelWidth2+RUN_SPACE*2)) {
        NSLog(@"第二条展示完成");
        //第二条展示完成
        self.moveLabel.x -= self.speed;
        _moveLabel2.x = CGRectGetMaxX(self.moveLabel.frame)+RUN_SPACE;
        _offsetX = 0;
    }
    NSLog(@"\noffset:%f----label1:%f-----%f\n",_offsetX,self.moveLabel.x,self.moveLabel2.x);
    
}

-(UILabel *)moveLabel{
    if (!_moveLabel) {
        _moveLabel = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, self.frame.size.width, self.frame.size.height)];
        _moveLabel.font = [UIFont boldSystemFontOfSize:18];
        _moveLabel.textColor = UIColor.whiteColor;
//        _moveLabel.backgroundColor = [UIColor yellowColor];
    }
    return _moveLabel;
}
-(UILabel *)moveLabel2{
    if (!_moveLabel2) {
        _moveLabel2 = [[UILabel alloc]initWithFrame:CGRectMake(CGRectGetMaxX(self.frame), 0, self.frame.size.width, self.frame.size.height)];
        _moveLabel2.font = [UIFont boldSystemFontOfSize:18];
        _moveLabel2.textColor = UIColor.whiteColor;
//        _moveLabel2.backgroundColor = [UIColor cyanColor];
    }
    return _moveLabel2;
}
-(void)dealloc{
    [self.link invalidate];
    self.link = nil;
}

@end
